home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicOptionPaneUI.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  266 lines

  1. /*
  2.  * @(#)OrganicOptionPaneUI.java    1.4 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.BasicOptionPaneUI;
  26. import com.sun.java.swing.plaf.ComponentUI;
  27. import java.awt.Color;
  28. import java.awt.*;
  29. import java.awt.event.*;
  30.  
  31.  
  32. /**
  33.  * Provides the Organic Look and Feel for a JOptionPane.
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @version 1.4 02/02/98
  43.  * @author Tom Santos
  44.  */
  45. public class OrganicOptionPaneUI extends BasicOptionPaneUI
  46. {
  47.     protected final static Dimension MINIMUM_SIZE = new Dimension( 300, 180 );
  48.  
  49.     public static ComponentUI createUI(JComponent x) {
  50.     return new OrganicOptionPaneUI();
  51.     }
  52.  
  53.     public void installUI( JComponent c ) {
  54.         super.installUI( c );
  55.     }
  56.  
  57.     /**
  58.      * Fills the backround if the component is opaque.
  59.      */
  60.   /*  public void paint(Graphics g, JComponent c) {
  61.     if(c.isOpaque()) {
  62.         g.setColor(c.getBackground());
  63.         g.fillRect(0, 0, c.getWidth(), c.getHeight());
  64.     }
  65.     }*/
  66.   
  67.     protected Container createBody() {
  68.     Container          top = new Panel();
  69.  
  70.     top.setLayout( new BorderLayout() );
  71.  
  72.     GridBagConstraints cons = new GridBagConstraints();
  73.     Container          body = new Container() {};
  74.     Container          realBody = new Container() {};
  75.  
  76.     realBody.setLayout(new BorderLayout());
  77.     realBody.add( createButtons(), BorderLayout.SOUTH );
  78.     realBody.add(new Container() {
  79.         public Dimension getPreferredSize() {
  80.         return new Dimension(15, 1);
  81.         }
  82.     }, BorderLayout.WEST);
  83.     realBody.add(body, BorderLayout.CENTER);
  84.  
  85.     body.setLayout(new GridBagLayout());
  86.     cons.gridx = cons.gridy = 0;
  87.     cons.gridwidth = GridBagConstraints.REMAINDER;
  88.     cons.gridheight = 1;
  89.     cons.anchor = GridBagConstraints.WEST;
  90.     cons.insets = new Insets(0,0,3,0);
  91.  
  92.     appendDescription(body, cons, getMessage(),
  93.               getMaxCharactersPerLineCount(), false);
  94.     top.add(realBody, BorderLayout.CENTER);
  95.  
  96.     addIcon( top );
  97.  
  98.     return top;
  99.     }
  100.  
  101.     /**
  102.      * Creates and returns a Container containin the buttons. The buttons
  103.      * are created by calling <code>getButtons</code>.
  104.      */
  105.     protected Container createButtons() {
  106.     /* And the bottom for all the buttons. */
  107.         Container             buttons = new Panel() {
  108.         private final Insets insets = new Insets( 0, 10, 15, 0 );
  109.         public Insets getInsets() {
  110.             return insets;
  111.         }
  112.     };
  113.  
  114.     FlowLayout layout = new FlowLayout();
  115.     layout.setAlignment( FlowLayout.LEFT );
  116.     buttons.setLayout( layout );
  117.     appendButtons(buttons, getButtons(), getInitialIndex());
  118.  
  119.     return buttons;
  120.     }
  121.  
  122.     /**
  123.      * Creates the appropriate object to represent each of the objects in
  124.      * <code>buttons</code> and adds it to <code>container</code>. This
  125.      * differs from appendDescription in that it will recurse on
  126.      * <code>buttons</code> and that if button is not a Component
  127.      * it will create an instance of JButton, that when pressed will
  128.      * invoke <code>createdButtonFired</code> with the appropriate
  129.      * index.
  130.      */
  131.     protected void appendButtons(Container container, Object[] buttons,
  132.                  int initialIndex) {
  133.     if(buttons != null && buttons.length > 0) {
  134.         boolean            sizeButtonsToSame = getSizeButtonsToSameWidth();
  135.         boolean            createdAll = true;
  136.         int                numButtons = buttons.length;
  137.         JButton[]          createdButtons = null;
  138.         int                maxWidth = 0;
  139.  
  140.         if(sizeButtonsToSame) {
  141.         createdButtons = new JButton[numButtons];
  142.          }
  143.  
  144.         for(int counter = 0; counter < numButtons; counter++) {
  145.         Object       anO = buttons[counter];
  146.         Component    newComponent;
  147.  
  148.         if(anO instanceof Component) {
  149.             createdAll = false;
  150.             newComponent = (Component)anO;
  151.             container.add(newComponent);
  152.             hasCustomComponents = true;
  153.         } else {
  154.             JButton      aButton;
  155.  
  156.             if(anO instanceof Icon)
  157.             aButton = new JButton((Icon)anO);
  158.             else
  159.             aButton = new JButton(anO.toString());
  160.             container.add(aButton);
  161.  
  162.             final int       buttonIndex = counter;
  163.  
  164.             aButton.addActionListener(new ActionListener() {
  165.             public void actionPerformed(ActionEvent e) {
  166.                 createdButtonFired(buttonIndex);
  167.             }
  168.             });
  169.             newComponent = aButton;
  170.         }
  171.         if(sizeButtonsToSame && createdAll && 
  172.            (newComponent instanceof JButton)) {
  173.             createdButtons[counter] = (JButton)newComponent;
  174.             maxWidth = Math.max(maxWidth,
  175.                     newComponent.getMinimumSize().width);
  176.         }
  177.         if(counter == initialIndex) {
  178.             initialFocusComponent = newComponent;
  179.         }
  180.         }
  181.         //        ((SyncingLayoutManager)container.getLayout()).
  182.         //                  setSyncsAll((sizeButtonsToSame && createdAll));
  183.         /* Set the padding, windows seems to use 8 if <= 2 components,
  184.            otherwise 4 is used. It may actually just be the size of the
  185.            buttons is always the same, not sure. */
  186.         if(sizeButtonsToSame && createdAll) {
  187.         JButton               aButton;
  188.         int                   padSize;
  189.  
  190.         if(numButtons <= 2)
  191.             padSize = 8;
  192.         else
  193.             padSize = 4;
  194.         for(int counter = 0; counter < numButtons; counter++) {
  195.             aButton = createdButtons[counter];
  196.             aButton.setMargin(new Insets(2, padSize, 2, padSize));
  197.         }
  198.         }
  199.     }
  200.     }
  201.  
  202.     /**
  203.      * Removes all the components from the Container, adds the icon,
  204.      * message and buttons.
  205.      */
  206.     protected void validateComponent() {
  207.     Container        container = getContainer();
  208.  
  209.     hasCustomComponents = false;
  210.     initialFocusComponent = null;
  211.     if(container != null) {
  212.         emptyContainer(container);
  213.         container.setLayout( new BorderLayout() );
  214.         container.add(createBody(), BorderLayout.CENTER);
  215.     }
  216.     }
  217.  
  218.     /**
  219.      * Creates and adds a JLabel representing the icon returned from
  220.      * <code>getIcon</code> to <code>top</code>. This is messaged from
  221.      * <code>createBody</code>
  222.      */
  223.     protected void addIcon(Container top) {
  224.     /* Create the icon. */
  225.     Icon                  sideIcon = getIcon();
  226.  
  227.     if (sideIcon != null) {
  228.         JLabel            iconLabel = new JLabel(sideIcon);
  229.  
  230.         iconLabel.setVerticalAlignment(SwingConstants.CENTER);
  231.         Panel panel = new Panel() {
  232.             public void paint( Graphics g ) {
  233.             super.paint( g );
  234.             g.setColor( UIManager.getColor("OptionPane.darkShadow") );
  235.             Rectangle bounds = getBounds();
  236.             g.drawLine( bounds.x + bounds.width - 1, 0,
  237.                 bounds.x + bounds.width - 1, bounds.y + bounds.height );
  238.         }
  239.         public Insets getInsets() {
  240.             return new Insets( 0, 13, 0, 13 );
  241.         }
  242.         };
  243.         panel.setBackground( UIManager.getColor("OptionPane.shadow") );
  244.         panel.setLayout( new BorderLayout() );
  245.         panel.add(iconLabel, BorderLayout.CENTER);
  246.         top.add( panel, BorderLayout.WEST );
  247.     }
  248.     }
  249.  
  250.     /**
  251.      * Returns null, CDE/Motif does not impose a minimum size.
  252.      */
  253.     public Dimension getMinimumOptionPaneSize() {
  254.     return MINIMUM_SIZE;
  255.     }
  256.  
  257.     /**
  258.      * Returns the insets to be used for the body, the body contains both
  259.      * the image and the actual message.
  260.      */
  261.     protected Insets getBodyInsets() {
  262.     return new Insets(0, 0, 12, 0);
  263.     }
  264.  
  265. }
  266.